package com.zf.shuashua.ui.mine.notice;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.zf.shuashua.R;
import com.zf.shuashua.bean.NoticeBean;

import java.util.List;

public class NoticeListAdapter extends MeiBaseAdapter<NoticeBean> {

    private OnNoticeListener mListener;

    public NoticeListAdapter(int layoutResId, @Nullable List<NoticeBean> data) {
        super(layoutResId, data);
    }

    public void setListener(OnNoticeListener listener) {
        this.mListener = listener;
    }

    public interface OnNoticeListener {
        void onDetail(NoticeBean noticeBean);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final NoticeBean item) {
        helper.setText(R.id.tv_title, item.getTitle())
                .setText(R.id.tv_time, item.getTime());
        helper.addOnClickListener(R.id.tv_description);
        if (!TextUtils.isEmpty(item.getDescription_img())) {
            Glide.with(mContext).load(item.getDescription_img()).into((ImageView) helper.getView(R.id.imgDetails));
        }
        TextView tv_description = helper.getView(R.id.tv_description);
        if (item.getJump_status() == 2 && !TextUtils.isEmpty(item.getJump_info())) {
            try {
                tv_description.setText(getClickableSpan(item));
                //设置该句使文本的超连接起作用
                tv_description.setMovementMethod(LinkMovementMethod.getInstance());

            } catch (Exception ignored) {
            }
        } else {
            tv_description.setText(item.getDescription());
        }
    }

    //设置超链接文字
    private SpannableString getClickableSpan(NoticeBean item) {
        String jump_info = item.getJump_info();
        SpannableString spanStr = new SpannableString(item.getDescription() + jump_info);
        try {
            int start = (item.getDescription() + jump_info).indexOf(jump_info);
            int end = start + jump_info.length();
            //设置下划线文字
            spanStr.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            //设置文字的前景色
//        spanStr.setSpan(new ForegroundColorSpan(Color.parseColor("#FB5054")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //设置文字的单击事件
            spanStr.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    if (mListener != null) {
                        mListener.onDetail(item);
                    }
                }
            }, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        } catch (Exception ignored) {

        }
        return spanStr;
    }
}